package com.zj.fx.service;

import com.zj.fx.common.base.BaseService;
import com.zj.fx.common.support.ConvertUtil;
import com.zj.fx.mapper.auto.TSysDictDataMapper;
import com.zj.fx.mapper.auto.TSysDictTypeMapper;
import com.zj.fx.model.auto.TSysDictData;
import com.zj.fx.model.auto.TSysDictDataExample;
import com.zj.fx.model.auto.TSysDictType;
import com.zj.fx.model.custom.Tablepar;
import com.zj.fx.shiro.util.ShiroUtils;
import com.zj.fx.util.SnowflakeIdWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 字典数据表Service
 *
 * @author 一休
 * @Title: TSysDictDataService.java
 * @Package com.zj.fx.service
 * @email 438081243@qq.com
 * @date 2019-09-08 00:10:43
 */
@Service
@RequiredArgsConstructor
public class SysDictDataService implements BaseService<TSysDictData, TSysDictDataExample> {
    private final TSysDictDataMapper tSysDictDataMapper;
    private final TSysDictTypeMapper tSysDictTypeMapper;

    /**
     * 分页查询
     *
     * @return
     */
    public PageInfo<TSysDictData> list(Tablepar tablepar, String name, String dictId) {
        TSysDictDataExample testExample = new TSysDictDataExample();
        testExample.setOrderByClause("dict_sort ASC");
        if (dictId != null && !"".equals(dictId)) {
            TSysDictType dictType = tSysDictTypeMapper.selectByPrimaryKey(dictId);
            if (dictType != null) {
                testExample.createCriteria().andDictTypeEqualTo(dictType.getDictType());
            }
        }
        if (name != null && !"".equals(name)) {
            testExample.createCriteria().andDictValueLike("%" + name + "%");
        }

        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        List<TSysDictData> list = tSysDictDataMapper.selectByExample(testExample);
        PageInfo<TSysDictData> pageInfo = new PageInfo<TSysDictData>(list);
        return pageInfo;
    }


    @Override
    public int deleteByPrimaryKey(String ids) {
        List<String> lista = ConvertUtil.toListStrArray(ids);
        TSysDictDataExample example = new TSysDictDataExample();
        example.createCriteria().andIdIn(lista);
        return tSysDictDataMapper.deleteByExample(example);
    }

    /**
     * 添加
     */
    @Override
    public int insertSelective(TSysDictData record) {
        //添加雪花主键id
        record.setId(SnowflakeIdWorker.getUUID());
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());
        record.setCreateBy(ShiroUtils.getUser().getUsername());
        return tSysDictDataMapper.insertSelective(record);
    }

    @Override
    public TSysDictData selectByPrimaryKey(String id) {
        return tSysDictDataMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(TSysDictData record) {
        record.setUpdateTime(new Date());
        record.setUpdateBy(ShiroUtils.getUser().getUsername());
        return tSysDictDataMapper.updateByPrimaryKeySelective(record);
    }


    @Override
    public int updateByExampleSelective(TSysDictData record, TSysDictDataExample example) {

        return tSysDictDataMapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(TSysDictData record, TSysDictDataExample example) {

        return tSysDictDataMapper.updateByExample(record, example);
    }

    @Override
    public List<TSysDictData> selectByExample(TSysDictDataExample example) {

        return tSysDictDataMapper.selectByExample(example);
    }


    @Override
    public long countByExample(TSysDictDataExample example) {

        return tSysDictDataMapper.countByExample(example);
    }


    @Override
    public int deleteByExample(TSysDictDataExample example) {

        return tSysDictDataMapper.deleteByExample(example);
    }

    /**
     * 检查name
     *
     * @return
     */
    public int checkNameUnique(TSysDictData tSysDictData) {
        TSysDictDataExample example = new TSysDictDataExample();
        example.createCriteria().andDictValueEqualTo(tSysDictData.getDictValue());
        List<TSysDictData> list = tSysDictDataMapper.selectByExample(example);
        return list.size();
    }

}
